﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using OutSource.Common.MarketSense;
using QianJin.Models;
using System.Configuration;
using System.Web.Configuration;

namespace QianJin
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            
            routes.MapRoute(
                "Default",
                "{controller}.x/{action}/{id}",
                new { action = "Index", id = "" }
              );

            routes.MapRoute(
              "Root",
              "",
              new { controller = "Home", action = "Index", id = "" }
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);

            MarketSense.Init("en-us", new string[] { "en-us", "zh-cn" });

            ProductDatabase.Init(Server.MapPath("~/Config/ProductDatabase.xml"), Server.MapPath("~"));


            int port = int.Parse(WebConfigurationManager.AppSettings["port"]);
            string smtp = WebConfigurationManager.AppSettings["smtp"];
            string user = WebConfigurationManager.AppSettings["user"];
            string to = WebConfigurationManager.AppSettings["to"];
            string pwd = WebConfigurationManager.AppSettings["pwd"];

            UserInfo.Init(smtp, user, pwd, port, to);

        }

        void Session_Start(object sender, EventArgs e)
        {
            Session["user"] = new UserInfo();
        }

        void Application_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            UserInfo user = (UserInfo)Session["user"];
            user.Market = MarketSense.GetCurrentCulture(user.Market, this.Context);
            MarketSense.InitCurrentCulture(user.Market);
        }
    }
}